﻿local M = {}

local PlayerEx = CS.PlayerEx
local Tools = CS.Tools
local jsonData = CS.jsonData
local GUIPackage = CS.GUIPackage

---@param itemID integer
---@param count integer
---@param seid? userdata
---@param showText? boolean
local function addItem(itemID, count, seid, showText)
    seid = seid or Tools.CreateItemSeid(itemID)
    -- 须分开调用，可能XLua不允许将nil视作可选参数以提供默认值，因为nil被视为null参数时可以防止被改写成默认值
    if showText == nil then
        PlayerEx.Player:addItem(itemID, count, seid)
    else
        PlayerEx.Player:addItem(itemID, count, seid, showText)
    end
end

---@param skillID integer
local function addHasSkill(skillID)
    PlayerEx.Player:addHasSkillList(skillID)
end

---@param skillID integer
local function addHasStaticSkill(skillID)
    local player = PlayerEx.Player
    local firstAdd = true
    for _, hasStaticSkill in pairs(player.hasStaticSkillList) do
        if hasStaticSkill.itemId == skillID then
            firstAdd = false
            break
        end
    end
    if firstAdd then
        player:addHasStaticSkillList(skillID)
        GUIPackage.StaticSkill(Tools.instance:getStaticSkillKeyByID(skillID), 0, 5):Puting(player, player, 3)
    end
end

---@param skillID integer
---@param level? integer
local function staticSkillLevelUp(skillID, level)
    level = level or 1
    local player = PlayerEx.Player
    for _, hasStaticSkill in pairs(player.hasStaticSkillList) do
        if hasStaticSkill.itemId == skillID then
            while level > 0 do
                if hasStaticSkill.level < 5 then
                    hasStaticSkill.level = hasStaticSkill.level + 1
                    GUIPackage.StaticSkill(Tools.instance:getStaticSkillKeyByID(skillID), 0, 5):Puting(player, player, 3)
                else
                    break
                end
                level = level - 1
            end
            break
        end
    end
end

function M.run(runner, env)
    -- 玩家拥有天赋【殇道初明】：悟道点+8
    if PlayerEx.HasTianFu(690001) then
        PlayerEx.Player.WuDaoDian = PlayerEx.Player.WuDaoDian + 8
    end

    -- 玩家拥有天赋【固灵根】：五系权重+50
    if PlayerEx.HasTianFu(690011) then
        for i = 0, 4, 1 do
            PlayerEx.Player:SetLingGen(i, PlayerEx.Player.LingGeng[i] + 50)
        end
    end

    -- 玩家拥有天赋【炼丹狂魔】：
    -- 初始丹道感悟境界达到初窥门径(+5000)
    -- 初始丹道领悟识药、剔毒、控火、抗毒
    if PlayerEx.HasTianFu(690021) then
        PlayerEx.Player.wuDaoMag:addWuDaoEx(21, 5000)
        for _, id in ipairs({ 2101, 2111, 2121, 2122 }) do
            if PlayerEx.Player.wuDaoMag:addWuDaoSkill(21, id) then
                PlayerEx.Player.WuDaoDian = PlayerEx.Player.WuDaoDian + jsonData.instance.WuDaoJson:TryGetField(tostring(id)):TryGetField("Cast").I
            end
        end
    end

    -- 玩家拥有天赋【炼器狂魔】：
    -- 初始器道感悟境界达到初窥门径(+5000)
    -- 初始器道领悟朱雀、白虎、青龙、增速
    if PlayerEx.HasTianFu(690031) then
        PlayerEx.Player.wuDaoMag:addWuDaoEx(22, 5000)
        for _, id in ipairs({ 2201, 2202, 2211, 2221 }) do
            if PlayerEx.Player.wuDaoMag:addWuDaoSkill(22, id) then
                PlayerEx.Player.WuDaoDian = PlayerEx.Player.WuDaoDian + jsonData.instance.WuDaoJson:TryGetField(tostring(id)):TryGetField("Cast").I
            end
        end
    end

    -- 玩家拥有天赋【布阵狂魔】：
    -- 初始阵道感悟境界达到初窥门径(+5000)
    -- 初始阵道领悟奇门、八卦、聚灵、两仪
    -- 初始学会《聚灵阵》
    if PlayerEx.HasTianFu(690041) then
        PlayerEx.Player.wuDaoMag:addWuDaoEx(10, 5000)
        for _, id in ipairs({ 1001, 1011, 1022, 1031 }) do
            if PlayerEx.Player.wuDaoMag:addWuDaoSkill(10, id) then
                PlayerEx.Player.WuDaoDian = PlayerEx.Player.WuDaoDian + jsonData.instance.WuDaoJson:TryGetField(tostring(id)):TryGetField("Cast").I
            end
        end
        addHasSkill(801)
    end

    -- 玩家拥有天赋【天生敏足】：
    -- 初始学会《轻身术》和《罗烟步》
    if PlayerEx.HasTianFu(690071) then
        addHasSkill(541)
        addHasStaticSkill(801)
    end

    -- 玩家拥有天赋【驭X灵体】：
    -- 初始X之道悟道经验+1000
    if PlayerEx.HasTianFu(690091) then PlayerEx.Player.wuDaoMag:addWuDaoEx(1, 1000) end
    if PlayerEx.HasTianFu(690101) then PlayerEx.Player.wuDaoMag:addWuDaoEx(2, 1000) end
    if PlayerEx.HasTianFu(690111) then PlayerEx.Player.wuDaoMag:addWuDaoEx(3, 1000) end
    if PlayerEx.HasTianFu(690121) then PlayerEx.Player.wuDaoMag:addWuDaoEx(4, 1000) end
    if PlayerEx.HasTianFu(690131) then PlayerEx.Player.wuDaoMag:addWuDaoEx(5, 1000) end

    -- 玩家拥有天赋【大堆药材】：
    -- 初始获得大量一品药材
    if PlayerEx.HasTianFu(690151) then
        for id = 6001, 6007, 1 do
            addItem(id, 500)
        end
    end

    -- 玩家拥有天赋【能工巧匠】：
    -- 初始学会《驱物术》
    if PlayerEx.HasTianFu(690161) then
        addHasSkill(518)
    end

    -- 玩家拥有天赋【初窥五行】：
    -- 初始五行的大道感悟境界达到初窥门径（+3000）
    -- 初始领悟御金、御水、御木、御火、御土
    if PlayerEx.HasTianFu(690181) then
        for _, i in ipairs({ 1, 3, 2, 4, 5 }) do
            PlayerEx.Player.wuDaoMag:addWuDaoEx(i, 3000)
        end
        for _, data in ipairs({ { 1, 101 }, { 2, 201 }, { 3, 301 }, { 4, 401 }, { 5, 501 }, }) do
            if PlayerEx.Player.wuDaoMag:addWuDaoSkill(data[1], data[2]) then
                PlayerEx.Player.WuDaoDian = PlayerEx.Player.WuDaoDian + jsonData.instance.WuDaoJson:TryGetField(data[2]):TryGetField("Cast").I
            end
        end
    end

    -- 玩家拥有天赋【航海士】：
    -- 海上声望+50
    if PlayerEx.HasTianFu(690191) then
        addItem(11901, 1)
        PlayerEx.AddSeaShengWang(50)
    end

    -- 玩家拥有天赋【镇静如山】：
    -- 初始学会《冰心诀》3级
    if PlayerEx.HasTianFu(690251) then
        addHasStaticSkill(510)
        staticSkillLevelUp(510, 2)
    end

    -- 妖灵系列
    if PlayerEx.HasTianFu(690271) then addHasStaticSkill(690273) end -- 覆铠
    if PlayerEx.HasTianFu(690291) then addHasStaticSkill(690293) end -- 韧甲
    if PlayerEx.HasTianFu(690311) then addHasStaticSkill(690313) end -- 霜甲
    if PlayerEx.HasTianFu(690331) then addHasStaticSkill(690333) end -- 结甲
    if PlayerEx.HasTianFu(690351) then addHasStaticSkill(690353) end -- 御土
    if PlayerEx.HasTianFu(690371) then addHasStaticSkill(690373) end -- 龙甲
end

return M
